<?php
//error_reporting(0);
require_once 'Swift/lib/swift_required.php'; 
require_once( 'DBConfig.php' );  
require('smarty/mysmarty.php');
session_start();

$ziriun=$_SESSION["adminusername"];

$link = mysql_connect($AdminHost, $AdminUser, $AdminPassword);
$aDB =mysql_select_db($AdminDatabase , $link)or die( "Sesija je istekla");


$smarty = new My_smarty();
$unosporuke = $_POST['unosporuke'];
$poruka = strip_tags($_POST['poruka']);
$naslov = strip_tags($_POST['naslov']);
if ($ziriun!="" and $unosporuke=="da")
{
    $rez1 = mysql_query("select username,rejting from autori");
    $brrez=mysql_num_rows($rez1);
    for ($i = 0; $i < $brrez; $i++) 
    {
        $kome=mysql_result($rez1,$i,"username");
        if ($naslov=="")
            $naslov="bez naslova";
        $aSQL  = "insert into privatneporuke (id, id_razgovor, procitana, usernameod, usernamekome, datum_vreme_upisa, poruka, naslov ) values ('','0','0','Administrator','$kome',now(),'$poruka','$naslov')";
        mysql_query($aSQL) or die(mysql_error());
    //Slanje maila PM***************************************************************
        $unzamail=$kome;
        $rezzamail1 = mysql_query("select email,mailnot from autori where username = '$unzamail'");
        $email= mysql_result($rezzamail1,0,"email");
        $emailnot= mysql_result($rezzamail1,0,"mailnot");
        if ($emailnot==0 and $unzamail!=$username and $email!="")
        {
        $vlink="Ovaj mail je poslat sa www.tattooportal.org   <br><br>
            Imate novu privatnu poruku na sajtu TattooPortal<br>
             <br>
            <br>
            <a href='http://www.tattooportal.org/privatneporuke.php'>Pročitajte Vašu poruku</a>
            <br><br><br>
            Ova poruka je automatski generisana, nemojte odgovarati na ovaj email.";
        $email_subject = "Nova privatna poruka";
        $email_contents = "<html>";
        $email_contents .= "<h2>www.tattooportal.org</h2>";
        $email_contents .= "<br>";
        $email_contents .= "<br>" . $vlink;
        $email_contents .= "</html>";

        $transport = Swift_SmtpTransport::newInstance('mail.tattooportal.org', 2626)
            ->setUsername('admin@tattooportal.org')
            ->setPassword('=?KD@M76')
            ;        
        $mailer = Swift_Mailer::newInstance($transport);

        $message = Swift_Message::newInstance($email_subject)
          ->setFrom(array('admin@tattooportal.org' => 'Tattoo portal'))
          ->setTo($email)
          ->setBody($email_contents, 'text/html')
          ;
          
        //Send the message
        $result = $mailer->send($message); 
        }  
        //**********************************************************************************************     
    }
	$smarty->assign('poruka',"Poruke su poslate");
}

//echo $ziriun;
if ($ziriun=="")
{
    $smarty->display('login.tpl'); 
    exit;
}
else
{
    $smarty->assign('username',$ziriun); 
    $smarty->display('index.tpl'); 
    exit; 
}
if ($sta=="")
{
    $smarty->display('login.tpl'); 
    exit;
}
if ($sta=="thumbs")
{
    
}
$smarty->assign('sta',$sta);  
$smarty->display('index.tpl');

?>